#pragma once


#include "Glad/glad/glad.h"
#include "Glad/GLFW/glfw3.h"

#include <iostream>
#include <string>

#include "Includes\Component.h"
#include "Tools\Shader.h"
#include "Tools\stb_image.h"
namespace SakuraAge {
class SpriteRenderer : public Component,
                       public RendererControl,
                       public RefBase<SpriteRenderer> {
  friend class Component;
  friend class RefBase<SpriteRenderer>;

 private:
  SpriteRenderer();
  virtual ~SpriteRenderer();
 protected:
  ObjRef<Shader> shader = nullptr;
  float* vertices = nullptr;
  ObjRef<Texture> tex = nullptr;

 public:
  std::string texturePath;
  virtual void Awake();
  void Load();
  virtual void OnRenderer();
  virtual void Serialize(SerializedStream& ss);
};
void SpriteRenderer::Serialize(SerializedStream& ss) {
  Component::Serialize(ss);
  ss&(ObjectRef&)tex;
  Load();
}
void SpriteRenderer::Awake() { Load(); }
void SpriteRenderer::Load() {
  vertices = new float[24]{
      // vertices			texture
      0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0};
  if (!texturePath.empty()) {
    tex = new Texture(texturePath,
                      Texture::TextureMode(Texture::SurroundMod::ClampToEdge,
                                           Texture::Resolution::Linear));
    shader = new Shader("Sprite", false, tex);
  }
}
void SakuraAge::SpriteRenderer::OnRenderer() {
  if (texturePath.empty()) {
    return;
  } else if (shader == nullptr) {
    tex = new Texture(texturePath,
                      Texture::TextureMode(Texture::SurroundMod::ClampToEdge,
                                           Texture::Resolution::Linear));
    shader = new Shader("Sprite", false, tex);
  }
  float sin_theta, cos_theta;
  sin_theta = sinf(Component::item->transform->rotation);
  cos_theta = cosf(Component::item->transform->rotation);

  Vector2 p = item->transform->position;
  Vector2 s = item->transform->scale;
  Color c = *BackGroundColor;
  shader->SetColor("BGColor", c.r, c.g, c.b, c.a);
  Vector2 p1 = Vector2(p.x + s.y * sin_theta - s.x * cos_theta,
                       p.y - s.x * sin_theta - s.y * cos_theta),
          p2 = Vector2(p.x + s.x * cos_theta + s.y * sin_theta,
                       p.y + s.x * sin_theta - s.y * cos_theta),
          p3 = Vector2(p.x - s.x * cos_theta - s.y * sin_theta,
                       p.y + s.y * cos_theta - s.x * sin_theta),
          p4 = Vector2(p.x - s.y * sin_theta + s.x * cos_theta,
                       p.y + s.x * sin_theta + s.y * cos_theta);
  p1 = GetStandardPosition(p1);
  p2 = GetStandardPosition(p2);
  p3 = GetStandardPosition(p3);
  p4 = GetStandardPosition(p4);

  // p1
  vertices[0] = p1.x;
  vertices[1] = p1.y;

  // p2
  vertices[4] = p2.x;
  vertices[5] = p2.y;
  vertices[16] = p2.x;
  vertices[17] = p2.y;

  // p3
  vertices[8] = p3.x;
  vertices[9] = p3.y;
  vertices[20] = p3.x;
  vertices[21] = p3.y;

  // p4
  vertices[12] = p4.x;
  vertices[13] = p4.y;

  shader->Render(vertices, 24, nullptr, 6);
}
SpriteRenderer::SpriteRenderer():Component(),RendererControl() {}
SpriteRenderer::~SpriteRenderer() {}
}  // namespace SakuraAge
